package com.ruoyi.littleRedBook.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.littleRedBook.domain.pojo.Category;
import com.ruoyi.littleRedBook.mapper.CategoryMapper;
import com.ruoyi.littleRedBook.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public AjaxResult getEnabledCategoryList() {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1) // 只查询启用状态的分类
                    .orderByAsc("parent_id", "sort_order"); // 按父ID和排序号排序
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        return AjaxResult.success(categoryList);
    }

    @Override
    public AjaxResult getChildCategoryList(Long parentId) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId)
                    .eq("status", 1)
                    .orderByAsc("sort_order");
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        return AjaxResult.success(categoryList);
    }

    @Override
    public AjaxResult getCategoryById(Long id) {
        Category category = categoryMapper.selectById(id);
        if (category == null || category.getStatus() != 1) {
            return AjaxResult.error("分类不存在或已禁用");
        }
        return AjaxResult.success(category);
    }
}
